package abq2.jic.jic.plugin.operate.library;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.form.gpt.IGPTAction;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

public class FeedbackQuery implements IGPTAction {
    @Override
    public Map<String, String> invokeAction(String action, Map<String, String> params) {
        Map<String , String> result = new HashMap<>();
        if ("GET_Feedback_Info".equalsIgnoreCase(action)) {
            //获取DynamicObject列表，即获取学生的单据头的信息
            DynamicObject[] dys = BusinessDataServiceHelper.load("abq2_book_feedback", //反馈单据
                            "abq2_datefield," +         //反馈日期
                            "billno," + //单据编号
                            "abq2_entryentity," +  //建议采购书籍单据体
                            "abq2_textfield2," + //建议采购书籍名称
                            "abq2_textfield3," +  //建议采购书籍的作者名
                            "abq2_textfield4," +   //建议采购书籍的书籍类型
                            "abq2_textareafield," +  //关于建议采购书籍的说明
                            "abq2_largetextfield",           //反馈内容
                    (new QFilter("billno", QCP.is_notnull, null)).toArray());

            //创建一个JsonArray
            JSONArray jsonArray = new JSONArray();
            for (DynamicObject dynamicObject : dys) {
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("feedback_time", new SimpleDateFormat("yyyy-MM-dd").format(dynamicObject.getDate("abq2_datefield")));
                    jsonObject.put("feedback_information", dynamicObject.getString("abq2_largetextfield"));
                    jsonArray.add(jsonObject);
                    //获取学生的成绩信息中的单据体信息，即获取学生的各门科目的成绩信息
                    int num = 0;
                    DynamicObjectCollection dynamicObjectCollection = dynamicObject.getDynamicObjectCollection("abq2_entryentity");
                    for (DynamicObject dynamicObject2 : dynamicObjectCollection) {
                        if (num > 5){
                            break;
                        }
                        //将各科目的的信息加入JSONArray
                        JSONObject jsonObject2 = new JSONObject();
                        jsonObject2.put("book_name", dynamicObject2.getString("abq2_textfield2"));  //书籍名称
                        jsonObject2.put("author_name", dynamicObject2.getString("abq2_textfield3"));  //书籍作者名
                        jsonObject2.put("book_type", dynamicObject2.getString("abq2_textfield4"));  //书籍类型
                        jsonObject2.put("book_info", dynamicObject2.getString("abq2_textareafield"));  //书籍说明
                        jsonArray.add(jsonObject2);
                        num++;
                    }
            }


            System.out.println(jsonArray.toJSONString());
            //加入resultDynamicObject参数，将JsonArray加入到这个参数当中，然后返回
            result.put("resultDynamicObject", jsonArray.toJSONString());
        }
        return result;
    }
}